package com.zym.utils.EasyExcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.springframework.stereotype.Component;

@Component
public class ExcelConverter implements Converter<Integer> {
    private static final String NORMAL = "正常";
    private static final String UNNORMAL = "不正常";

    @Override
    public Class<?> supportJavaTypeKey() {
        // 实体类中对象属性类型
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        // Excel中对应的CellData属性类型
        return CellDataTypeEnum.STRING;
    }


    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value=cellData.getStringValue();
        if(value.equals("正常")){
            return 1;
        }else if(value.equals("停用")){
            return 0;
        }else {
            return null;
        }

    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer integer, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String newValue="";
        if(integer==1){
            newValue="正常";
        }else if (integer==0){
            newValue="停用";
        }
        return new WriteCellData<>(newValue);
    }



}
